package com.exam.controller.exam;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.exam.common.vo.ExamStuVO;
import com.exam.common.vo.ExamSubjectVO;
import com.exam.common.vo.ExamTiVO;
import com.exam.common.vo.GradeVO;
import com.exam.common.vo.page.PageModel;
import com.exam.common.vo.page.SystemContext;
import com.exam.entity.ExamGrade;
import com.exam.service.exam.ExamService;
import com.exam.service.grade.GradeService;
import com.exam.service.subject.SubjectService;
import com.exam.service.ti.TiService;

import net.sf.json.JSONObject;

@Controller
@RequestMapping("/exam")
public class ExamController {

	@Autowired
	TiService tiService;

	@Autowired
	ExamService examService;

	@Autowired
	SubjectService subService;

	@Autowired
	GradeService gradeService;

	@RequestMapping(value = "/examList/{subjectId}")
	public String firstTi(@PathVariable String subjectId, Model model, HttpSession session) {

		ExamTiVO tiVO = examService.getTi(1, null);
		ExamSubjectVO subVO = subService.findById(Integer.parseInt(subjectId));
		model.addAttribute("tiVO", tiVO);
		// System.out.println(tiVO.getExamTiId());
		Integer tNumber = 1;

		Map<Integer, ExamTiVO> tiList = new HashMap<Integer, ExamTiVO>();
		tiVO.setExamTiKeyChoose("");
		tiList.put(tNumber, tiVO);

		//System.out.println(tiVO.getExamTiKeyChoose());
		
		session.setAttribute("tNumber", tNumber);
		session.setAttribute("subVO", subVO);
		session.setAttribute("tiList", tiList);
		session.setAttribute("subjectId", subjectId);
		model.addAttribute("startExam", "start....");
		return "/exam/examList";
	}

	@SuppressWarnings("unchecked")
	@RequestMapping(value = "/nxTi", method = RequestMethod.POST)
	public void nxTi(HttpSession session, HttpServletResponse response, HttpServletRequest request)
			throws IOException {
		String key = request.getParameter("key");
		Integer tNum = Integer.parseInt(request.getParameter("tNum"));
		Map<Integer, ExamTiVO> tiList = (Map<Integer, ExamTiVO>) session.getAttribute("tiList");
		List<Integer> tiIdList = new ArrayList<Integer>();

		Integer pretNum = (Integer) session.getAttribute("tNumber");
		
		for (ExamTiVO tVO : tiList.values()) {
			tiIdList.add(tVO.getExamTiId());
		}
		Integer rig =  (Integer) session.getAttribute("rig");//累计答对数
		Integer dif = (Integer) session.getAttribute("dif");//难度
		
		if(rig == null) rig = 0;
		if(dif == null) dif = 1;
		if(rig == 3&&dif<=3){
			dif++;
		}
		System.out.println("正确数:"+rig+",难度:"+dif+"key:"+key);
		
		ExamTiVO tiVO = null;
		if (tNum < 21) {
			if (tiList.get(tNum) == null) {
				tiVO = examService.getTi(dif, tiIdList);
				tiVO.setExamTiKeyChoose("");
				tiList.put(tNum, tiVO);
			}
		}
		JSONObject json = new JSONObject();
		ExamTiVO tVO = new ExamTiVO();
		
		int num = 0;
		String oper = request.getParameter("oper");
		if(oper == "next"||oper.equals("next")){//下一题
			tVO = tiList.get(tNum-1);
			num = tNum - 1;
		}else if(oper=="pre"||oper.equals("pre")){//上一题
			tVO = tiList.get(tNum+1);
			num = tNum + 1;
		}else{                               //保存当前选择答案
			tVO = tiList.get(tNum);
			num = tNum;
		}
		if(tVO.getExamTiKeyChoose() == ""||tVO.getExamTiKeyChoose().equals("")){
			tVO.setExamTiKeyChoose(key);
		}
		System.out.println("选项"+","+tVO.getExamTiKey()+","+tVO.getExamTiKeyChoose());
		
		if(num!=pretNum){
			if(key.equalsIgnoreCase(tVO.getExamTiKey())||key==tVO.getExamTiKey()){
				rig++;
			}else{
				rig = 0;
			}
		}
		
		System.out.println(tNum+","+tVO.getExamTiKeyChoose());
		
		ExamTiVO tO = tiList.get(tNum);
		json = JSONObject.fromObject(tO);
		json.put("tNum", tNum);
		json.put("key",tO.getExamTiKeyChoose());

		session.setAttribute("rig", rig);
		session.setAttribute("dif", dif);
		session.setAttribute("tNumber", tNum);
		session.setAttribute("tiList", tiList);
		response.setCharacterEncoding("utf-8");
		response.getWriter().write(json.toString());
	}
	
	
	
	@SuppressWarnings("unchecked")
	@RequestMapping(value = "/nextTi", method = RequestMethod.POST)
	public void nextTi(HttpSession session, HttpServletResponse response, HttpServletRequest request)
			throws IOException {
		String key = request.getParameter("key");
		Integer tNum = Integer.parseInt(request.getParameter("tNum"));
		Map<Integer, ExamTiVO> tiList = (Map<Integer, ExamTiVO>) session.getAttribute("tiList");
		List<Integer> tiIdList = new ArrayList<Integer>();
		
		for (ExamTiVO tVO : tiList.values()) {
			tiIdList.add(tVO.getExamTiId());
		}
		Integer pretNum = (Integer) session.getAttribute("tNumber");
		
		Integer rig =  (Integer) session.getAttribute("rig");//累计答对数
		Integer dif = (Integer) session.getAttribute("dif");//难度
		System.out.println(rig+","+dif);
		
		if(rig == null) rig = 1;
		if(dif == null) dif = 1;
		if(rig == 3&&dif<=3){
			dif++;
		}
		ExamTiVO tiVO = null;
		if (tNum <= 20) {
			if (tiList.get(tNum) == null) {
				tiVO = examService.getTi(1, tiIdList);
				tiVO.setExamTiKeyChoose("");
				tiList.put(tNum, tiVO);
			}
		}
		
		
		JSONObject json = new JSONObject();
		ExamTiVO tVO = new ExamTiVO();
		tVO = tiList.get(tNum);
		if(tVO.getExamTiKeyChoose() == ""||tVO.getExamTiKeyChoose().equals("")){
			tVO.setExamTiKeyChoose(key);
		}
		
		if(tNum!=pretNum){
			if(key.equalsIgnoreCase(tVO.getExamTiKey())){
				rig++;
			}else{
				rig = 0;
			}
		}
		System.out.println(rig);
		json = JSONObject.fromObject(tVO);
	
		System.out.println(tNum+","+key);

		json.put("tNum", tNum);
		json.put("key",tVO.getExamTiKeyChoose());
		
		

		session.setAttribute("tNumber", tNum);
		session.setAttribute("dif", dif);
		session.setAttribute("rig", rig);
		session.setAttribute("tiList", tiList);
		response.setCharacterEncoding("utf-8");
		response.getWriter().write(json.toString());
	}
	
	

	@SuppressWarnings("unchecked")
	@RequestMapping(value = "/count", method = RequestMethod.POST)
	public void count(HttpSession session, HttpServletResponse response, HttpServletRequest request)
			throws IOException {
		Map<Integer, ExamTiVO> tiList = (Map<Integer, ExamTiVO>) session.getAttribute("tiList");

		
		int count = 0;
		int co = 0;

		System.out.println(tiList.size());
		System.out.println(tiList.get(1).getExamTiKeyChoose());
		try {
			for(Integer i :tiList.keySet()){
				System.out.println("计分"+tiList.get(i).getExamTiKey()+","+tiList.get(i).getExamTiKeyChoose());
				String tiKey = tiList.get(i).getExamTiKey();
				String chooseKey = tiList.get(i).getExamTiKeyChoose();
				if(tiKey.equalsIgnoreCase(chooseKey)||tiKey.toUpperCase() == chooseKey.toUpperCase()){
					count += tiList.get(i).getExamTiPoint();
				}
			}
		} catch (Exception e1) {
			e1.printStackTrace();
		}
		ExamSubjectVO subVO = (ExamSubjectVO) session.getAttribute("subVO");
		ExamStuVO stuVO = (ExamStuVO) session.getAttribute("stuVO");
		ExamGrade grade = new ExamGrade();
		grade.setExamGradePoint(count);
		grade.setExamGradeFlag(1);
		grade.setExamGradeSum(subVO.getExamSubjectSum());
		grade.setExamSubjectId(subVO.getExamSubjectId());
		grade.setExamGradeTime(new Date());
		grade.setExamGradeRight(co);
		grade.setExamStuId(Integer.parseInt(String.valueOf(stuVO.getExamStuId())));
		try {
			gradeService.save(grade);
		} catch (Exception e) {
			e.printStackTrace();
		}

		JSONObject json = new JSONObject();
		json.put("count", count);
		if (tiList.size() < 20) {
			json.put("state", "0");
		}
		response.getWriter().write(json.toString());

	}
	@RequestMapping(value = "/grade")
	public String gradeList(Model model,HttpSession session){
		session.removeAttribute("tNumber");
		session.removeAttribute("tiList");
		session.removeAttribute("subVO");
		session.removeAttribute("subjectId");
		session.removeAttribute("rig");
		session.removeAttribute("dif");
		
		ExamStuVO stuVO = (ExamStuVO) session.getAttribute("stuVO");
		
		SystemContext.setPageSize(8);
		PageModel<GradeVO> page = gradeService.gradeList(stuVO);
		
		model.addAttribute("page",page);
		return "/exam/gradeList";
	}
}
